﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace QuantumQuest
{
    public partial class QQTestLevel2 : QQLevel
    {
        private DispatcherTimer _timer;
        private QQPhysics _phys;
        

        private double _degsRevolved;
        private QQVector2 _diffVec;

        private double _revolutionsToFinish = 2;




        private QQEarth _earth;
        private QQMoon _moon;
        private QQRocket _rocket;


        public QQTestLevel2()
        {
            InitializeLevel();
        }
        public override void ShutDown()
        {
            QQGlobal.KeysDown.RemoveRange(0, QQGlobal.KeysDown.Count);
            QQGlobal.Score += (int)(_rocket.Fuel * 100.0);
            //todo
        }
        public override void Begin()
        {
            InitializeComponent();

            InitializeTestLevel();
            _timer.Start();
        }
        public void InitializeTestLevel()
        {

            
            _timer = new DispatcherTimer();
            _timer.Interval = TimeSpan.FromSeconds(QQGlobal.FramerateSeconds);
            _timer.Tick += new EventHandler(_timer_Tick);

            _phys = new QQPhysics();


            _bodies = new QQList(3);

            _earth = new QQEarth(1000000000000000, new QQVector2(500, 400));
            _rocket = new QQRocket(3, 100000000, new QQVector2(500, 475));
            _moon = new QQMoon(1000000000000000, new QQVector2(100, 100));

            _rocket.VelocityVec.X = 35;

            _earth.IsStationary = true;
            _moon.IsStationary = true;

            _bodies.Add(_earth);
            _bodies.Add(_rocket);
            _bodies.Add(_moon);

            _diffVec = new QQVector2(0, 0);
            _diffVec = _rocket.PositionVec - _earth.PositionVec;


            for (int i = 0; i < _bodies.Count; i++)
            {
                TestLevelCanvas.Children.Add((QQBody)_bodies[i]);

            }
        }

        void _timer_Tick(object sender, EventArgs e)
        {

            Line redLine = new Line();
            redLine.X1 = _rocket.PositionVec.X;
            redLine.Y1 = _rocket.PositionVec.Y;
            redLine.X2 = _rocket.PositionVec.X + _rocket.ThrustVec.X * 35;
            redLine.Y2 = _rocket.PositionVec.Y + _rocket.ThrustVec.Y * 35;
            SolidColorBrush redBrush = new SolidColorBrush();
            redBrush.Color = Colors.Red;
            redLine.StrokeThickness = 1;
            redLine.Stroke = redBrush;
            TestLevelCanvas.Children.Add(redLine);


            FuelText.Text = Convert.ToString(_rocket.Fuel);
            RotText.Text = Convert.ToString(_rocket.ThrustVec.ThetaDeg);
            ThrustMagText.Text = Convert.ToString(_rocket.ThrustVec.Magnitude);

    
       
            if (_phys.Collide(_earth, _rocket)||_phys.Collide(_moon,_rocket))
            {
                //_isFinished = true;
            }



            
            if (Math.Abs(_degsRevolved) >= 360.0 * _revolutionsToFinish)
            {
                //_isFinished = true;
            }
            QQVector2 tempVec = new QQVector2(0, 0);
            tempVec = _rocket.PositionVec - _moon.PositionVec;
            _degsRevolved += Math.Abs(_diffVec.ThetaDeg - tempVec.ThetaDeg) / 2.0;
            _diffVec = _rocket.PositionVec - _earth.PositionVec;



            _phys.UpdateCollisions3(ref _bodies);

            for (int i = 0; i < _bodies.Count; i++)
            {

                ((QQBody)(_bodies[i])).DynamicsParameters = (_phys.ApplyGravity2(_bodies, i, QQGlobal.Dt)).DynamicsParameters;

                ((QQBody)(_bodies[i])).Update();


            }
                
        }

    }
}
